const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const merge = require('webpack-merge');
const commonConfig = require('./webpack.common');

const prodConfig = {
  mode: 'production',
  devtool: 'cheap-module-source-map',
  module: {
    rules: [
      {
        test: /\.less$/,
        use: [
          MiniCssExtractPlugin.loader,
          {
            loader: 'css-loader',
            options: {
              importLoaders: 2,
            }
          },
          'less-loader',
          {
            loader: 'postcss-loader',
            options: {
              plugins: [
                require('autoprefixer')
              ]
            }
          }
        ]
      },
      {
        test: /\.css$/,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader'
        ]
      },
      {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        loader: 'babel-loader'
      }
    ]
  },
  output: {
    filename: 'bundle.[contenthash].js',
    chunkFilename: '[name].[contenthash].chunk.js',
  }, 
  plugins: [
    // css代码分割
    new MiniCssExtractPlugin({
      filename: 'style.[contenthash].css',
      chunkFilename: '[name].[contenthash].chunk.css'
    })
  ],
  optimization: {
    // css代码压缩和合并
    minimizer: [new OptimizeCSSAssetsPlugin({})],
  },
}

module.exports = merge(commonConfig, prodConfig);